<template>
	<view class="container">
		<uni-nav-bar class="ch-nav" height="88rpx" backgroundColor="#ffffff" leftWidth="90rpx" rightWidth="90rpx" color="#333" :fixed="true" :padding="false" :border="false" @clickLeft="$goBack(1)">
			<template slot="left">
				<image class="left" src="@/public/images/public/dhl_fhjt.png" mode="aspectFit"></image>
			</template>
			<text class="title">我的收藏</text>
		</uni-nav-bar>
		<view class="head" :class="navBorder ? 'navborder':''">
			<view class="tab flex-r ac">
				<view class="tab-list flex-r ac jc" v-for="(item,index) in tabList" :class="{'active': tabCur == index}" :key="index" @tap="tabChange(index)">
					<text class="text-primary">{{item.name}}</text>
				</view>
			</view>
		</view>
		<template v-for="(item, index) in tabList">
			<keep-alive>
			<view class="body bottom-ios" @touchstart="touchStart" @touchend="touchEnd" v-if="tabCur == index" :key="index">
				<EmptyContent v-if="!matchList[tabCur][0]" top="160rpx" text="暂无收藏的比赛" img="qsy_zwxl" @refresh="refresh"></EmptyContent>
				<MatchList v-else :sportId="sportId" :follow="true" :matchList="matchList[tabCur]" :isEnd="tabData[tabCur].isEnd"></MatchList>
			</view>
			</keep-alive>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				matchList: [[],[]],
				tabList: [
					{name: '足球'},{name: '篮球'}
				],
				tabData: [
					{pageNo: 1,	isEnd: false},
					{pageNo: 1,	isEnd: false}
				],
				tabCur: 0,
				navBorder: false
			}
		},
		components: {
			MatchList: () => import('@/components/ch-match/ch-matchlist.vue')
		},
		computed: {
			user() {
				return this.$store.state.user
			},
			sportId() {
				return this.tabCur+1
			},
			noFollow() {
				return this.$store.state.matchNoFollow
			}
		},
		watch: {
			tabCur(newValue, oldValue) {
				if(this.matchList[newValue]){
					return
				}
				this.getMatchList()
			},
			noFollow(newValue, oldValue) {
				newValue[this.tabCur].forEach(id => {
					let index = this.matchList[this.tabCur].findIndex(match => match.id == id)
					if(index > -1){
						this.matchList[this.tabCur].splice(index, 1)
					}
				})
			}
		},
		onLoad() {
			this.getMatchList()
		},
		onReachBottom() {
			if(!this.tabData[this.tabCur].isEnd){
				this.getMatchList()
			}
		},
		onPullDownRefresh() {
			this.tabData[this.tabCur].pageNo = 1
			this.getMatchList()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1000)
		},
		onPageScroll(e) {
			if(e.scrollTop > 10){
				this.navBorder = true
			}else{
				this.navBorder = false
			}
		},
		methods: {
			//刷新
			refresh() {
				this.tabData[this.tabCur].pageNo = 1
				this.getMatchList()
			},
			//获取赛事
			getMatchList(){
				//发送请求
				this.$http.get({
					url: '/api/queryCareMatchRows',
					data: {
						pageNo: this.tabData[this.tabCur].pageNo,
						pageSize: 15,
						sportId: this.sportId,
						userId: this.user.userId
					}
				}).then(data => {
					console.log(data);
					if(data.success && data.result.total){
						console.log(this.tabCur);
						if(data.result.current == 1){
							this.$set(this.matchList, this.tabCur, data.result.records)
						}else{
							this.matchList[this.tabCur].push(...data.result.records)
						}
						this.tabData[this.tabCur].isEnd = data.result.current >= data.result.pages
						this.tabData[this.tabCur].pageNo = data.result.current + 1
					}
				}).catch((err) => {
					console.log(err);
				})
			},
			//选项卡切换
			tabChange(index) {
				this.tabCur = index;
				if(!this.matchList[index][0]){
					this.getMatchList()
				}
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
			},
			//触摸滑动开始
			touchStart(e) {
				if (e.changedTouches.length == 1) {
					this.startX = e.changedTouches[0].clientX
					this.startY = e.changedTouches[0].clientY
				}
			},
			//触摸滑动结束
			touchEnd(e) {
				if (e.changedTouches.length == 1) {
					var endX = e.changedTouches[0].clientX;
					var endY = e.changedTouches[0].clientY;
					var xTowards = endX - this.startX;
					var yTowards = endY - this.startY;
					if (xTowards > 50 && Math.abs(yTowards) < 50) {//向右
						console.log('向右')
						if(this.tabCur > 0){
							this.tabChange(this.tabCur - 1)
						}
					}
					if (xTowards < -50 && Math.abs(yTowards) < 50) {//向左
						console.log('向左')
						let maxlen = this.tabData.length - 1;
						if(this.tabCur < maxlen){
							this.tabChange(this.tabCur + 1)
						}
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		.head{
			max-width: $max-width;
			margin: $pc-margin;
			width: 100%;
			position: fixed;
			background-color: #FFFFFF;
			background-image: url("~@/public/images/public/fgx.png");
			background-position: center center;
			background-repeat: no-repeat;
			background-size: auto 32rpx;
			z-index: 2;
			.tab{
				.tab-list{
					flex: 2;
					height: 88rpx;
					.text-primary{
						color: #666666;
					}
					&.active{
						.text-primary{
							font-weight: 500;
							color: #0055FF;
						}
						background-image: url("~@/public/images/public/zjdl_hk.png");
						background-repeat: no-repeat;
						background-position: bottom center;
						background-size: 60rpx 8rpx;
					}
				}
				.tab-list:first-child text{
					margin-right: 0;
				}
			}
		}
		
		.body{
			min-height: calc(var(--safe-height) - #{$navbar-height} - 88rpx - 20rpx);
			margin-top: 88rpx;
			padding-top: 20rpx;
		}
	}
</style>
